//
//  TTDetailSimilarityGoodsView.m
//  TaoTuo
//
//  Created by apple on 2020/11/16.
//  Copyright © 2020 刘广. All rights reserved.
//

#import "TTDetailSimilarityGoodsView.h"
#import "TTDetailSimilarityViewCell.h"
#import "TTGoodsDetailViewController.h"

@interface TTDetailSimilarityGoodsView ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UICollectionView *collectionView;


@end

@implementation TTDetailSimilarityGoodsView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = UIColor.whiteColor;
        self.layer.cornerRadius = 10;
        self.layer.masksToBounds = YES;
        [self setupSubviews];
        
    }
    return self;
}

- (void)setDataArray:(NSArray *)dataArray
{
    _dataArray = dataArray;
    [self.collectionView reloadData];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    TTHomeListModel *model = self.dataArray[indexPath.row];
    TTDetailSimilarityViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.model = model;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    TTHomeListModel *model = self.dataArray[indexPath.row];
    TTGoodsDetailViewController *detailVC = [[TTGoodsDetailViewController alloc] initWithStyleLibraryId:model.StyleLibraryId model:model];
    [[LLRouteTool getCurrentNavigationController] pushViewController:detailVC animated:YES];
}

- (void)setupSubviews
{
    _titleLabel = [UILabel labelWithTitle:@"相似商品" color:UIColorHex(333333) font:[UIFont boldSystemFontOfSize:16]];
    _titleLabel.frame = CGRectMake(10, 0, 120, 48);
    [self addSubview:_titleLabel];
    
    [self collectionView];
}


- (UICollectionView *)collectionView
{
    if (!_collectionView) {
        CGFloat itemW = (self.width - 40) / 3.0;
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.itemSize = CGSizeMake(itemW, 168);
        layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
        layout.minimumLineSpacing = 10;
        layout.minimumInteritemSpacing = 10;
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, self.titleLabel.bottom, self.width, self.height - self.titleLabel.bottom) collectionViewLayout:layout];
        _collectionView.backgroundColor = UIColor.whiteColor;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        [_collectionView registerClass:[TTDetailSimilarityViewCell class] forCellWithReuseIdentifier:@"cell"];
        
        [self addSubview:_collectionView];
    }
    return _collectionView;
}

@end
